package tech.lq0.mineapex.gui;

import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import tech.lq0.mineapex.armor.EvoShield;
import tech.lq0.mineapex.registry.ItemRegistry;

@OnlyIn(Dist.CLIENT)
public class ShieldHUD extends AbstractGui {
    public static final ResourceLocation SHIELD_HUD = new ResourceLocation("mineapex", "textures/gui/shield_hud.png");
    public static final ResourceLocation BATTERY_HUD = new ResourceLocation("mineapex", "textures/gui/battery_hud.png");

    public void render(RenderGameOverlayEvent.Post event, MatrixStack stack) {
        ClientPlayerEntity player = Minecraft.getInstance().player;
        if (player == null || player.isCreative()) return;
        ItemStack armor = player.getItemStackFromSlot(EquipmentSlotType.CHEST);

        MainWindow window = Minecraft.getInstance().getMainWindow();
        final int width = window.getScaledWidth();
        final int height = window.getScaledHeight();

        // 护甲显示
        if (!armor.isEmpty() && armor.getItem().equals(ItemRegistry.EVO_SHIED.get())) {
            float max, current, damage;
            if (armor.getStack().getChildTag("evo") == null) {
                max = EvoShield.shield[0];
                current = EvoShield.shield[0];
                damage = 0;
            } else {
                CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");
                max = nbt.getFloat("max");
                current = nbt.getFloat("current");
                damage = nbt.getFloat("damage");
            }
            max = Math.min(max, 1000);

            Minecraft.getInstance().getTextureManager().bindTexture(SHIELD_HUD);

            int padLeft = 0;
            int padBottom = 3;
            int w = 150;
            int h = 40;
            int textureWidth = 300;
            int textureHeight = 80;

            // 底部边框
            blit(stack, padLeft, height - padBottom - textureHeight / 2, w, h, 0, 0, textureWidth, textureHeight, textureWidth, textureHeight * 2);
            // 血条底
            blit(stack, padLeft + 61, height - padBottom - textureHeight / 2 + 32, 75, 5, 1, 81, 150, 10, textureWidth, textureHeight * 2);
            // 血条
            blit(stack, padLeft + 61, height - padBottom - textureHeight / 2 + 32, (int) (75 * Math.min(1, (player.getHealth() / player.getMaxHealth()))), 5, 1, 92, 150, 10, textureWidth, textureHeight * 2);

            int level = EvoShield.getLevel(armor);

            if (current == Float.POSITIVE_INFINITY) {
                // 彩虹甲甲条绘制
                int offset = (int) (System.currentTimeMillis() / 5 % 148);
                blit(stack, padLeft + 61 + 74 - offset / 2, height - padBottom - textureHeight / 2 + 26, offset / 2 + 1, 5, 151, 149, offset, 10, textureWidth, textureHeight * 2);
                blit(stack, padLeft + 61, height - padBottom - textureHeight / 2 + 26, 75 - offset / 2, 5, 151 + offset, 149, 148 - offset, 10, textureWidth, textureHeight * 2);
            } else {
                current = Math.min(current, 1000);

                // 护甲条底
                for (int i = 0; i < max / 25; i++) {
                    blit(stack, padLeft + 61 + 15 * i, height - padBottom - textureHeight / 2 + 26, 15, 5, 1, 103, 30, 10, textureWidth, textureHeight * 2);
                }
                // 护甲条
                int slots = (int) (current / 25);

                // 满格护甲绘制
                for (int i = 0; i < slots; i++) {
                    blit(stack, padLeft + 61 + 15 * i, height - padBottom - textureHeight / 2 + 26, 15, 5, 31 + 30 * level, 103, 30, 10, textureWidth, textureHeight * 2);
                }
                // 非满格护甲绘制
                int rest = (int) (current % 25);
                blit(stack, padLeft + 61 + 15 * slots, height - padBottom - textureHeight / 2 + 26, (int) ((rest / 25F) * 15), 5, 31 + 30 * level, 103, 30, 9, textureWidth, textureHeight * 2);
            }

            // 头像底
            blit(stack, padLeft + 23, height - padBottom - textureHeight / 2 + 4, 33, 33, 151, 81, 66, 66, textureWidth, textureHeight * 2);
            // 头盔 & 胸甲底
            blit(stack, padLeft + 109 - 3, height - padBottom - textureHeight / 2 + 9, 14, 14, 220, 82, 28, 28, textureWidth, textureHeight * 2);
            blit(stack, padLeft + 109 + 16 - 3, height - padBottom - textureHeight / 2 + 9, 14, 14, 220, 82, 28, 28, textureWidth, textureHeight * 2);
            // ID
            blit(stack, padLeft + 60, height - padBottom - textureHeight / 2 + 8, 44, 12, 50, 116, 88, 24, textureWidth, textureHeight * 2);
            // 进度条底
            blit(stack, padLeft + 61, height - padBottom - textureHeight / 2 + 5, 67, 2, 2, 148, 134, 2, textureWidth, textureHeight * 2);

            // 进度条
            if (level == EvoShield.shield.length - 1) {
                blit(stack, padLeft + 61, height - padBottom - textureHeight / 2 + 5, 67, 2, 2, 144, 134, 2, textureWidth, textureHeight * 2);
            } else {
                int last = 0;
                if (level >= 1) {
                    last = EvoShield.damage[level];
                }
                int next = EvoShield.damage[level + 1];
                int progress = (int) ((damage - last) / (next - last) * 67);
                blit(stack, padLeft + 61, height - padBottom - textureHeight / 2 + 5, progress, 2, 2, 144, 134, 2, textureWidth, textureHeight * 2);
            }

            // 玩家头像渲染
            Minecraft.getInstance().getTextureManager().bindTexture(player.getLocationSkin());
            blit(stack, padLeft + 24, height - padBottom - 31 - 4, 31, 31, 8, 8, 8, 8, 64, 64);
            blit(stack, padLeft + 24 - 3, height - padBottom - 31 - 4 - 3, 37, 37, 40, 8, 8, 8, 64, 64);

            // 头盔 & 胸甲 渲染
            ItemStack helmet = player.getItemStackFromSlot(EquipmentSlotType.HEAD);
            if (helmet != ItemStack.EMPTY) {
                Minecraft.getInstance().getItemRenderer().renderItemIntoGUI(helmet, padLeft + 109 - 1 - 3, height - padBottom - textureHeight / 2 + 9 - 1);
            }
            ItemStack chestplate = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
            if (chestplate != ItemStack.EMPTY) {
                Minecraft.getInstance().getItemRenderer().renderItemIntoGUI(chestplate, padLeft + 109 - 1 + 16 - 3, height - padBottom - textureHeight / 2 + 9 - 1);
            }
        }

        ItemStack item = player.getHeldItemMainhand();
        if (item.isEmpty() || player.getItemInUseCount() == 0) return;
        Item type = item.getItem();

        // 电池/治疗包使用时长显示
        if (type.equals(ItemRegistry.SHIELD_BATTERY.get()) || type.equals(ItemRegistry.SHIELD_CELL.get()) || type.equals(ItemRegistry.PHOENIX_KIT.get()) || type.equals(ItemRegistry.MED_KIT.get()) || type.equals(ItemRegistry.SYRINGE.get())) {
            int x = width / 2 - 50;
            int y = height / 2 - 5 + 60;
            Minecraft.getInstance().getTextureManager().bindTexture(BATTERY_HUD);
            // 边框渲染
            blit(stack, x, y, 0, 0, 100, 10, 100, 20);

            double max = player.getItemInUseCount() + player.getItemInUseMaxCount();
            double current = max - player.getItemInUseCount();
            // 进度条渲染
            blit(stack, x, y, 0, 10, (int) (100.0 * (current / max)), 10, 100, 20);
        }
    }
}
